package cn.com.netedge.iot.frontend.comm.nio;


import cn.com.netedge.iot.common.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseServer implements Runnable {

    protected int[] getPorts(String port) {
        //读取端口参数，允许配置多个端口
        if (!StringUtil.isEmpty(port)) {
            String[] portArray = port.split(",");
            if (portArray != null && portArray.length > 0) {
                int length = portArray.length;
                List<Integer> portList = new ArrayList<>(length);
                for (String item : portArray) {
                    if (StringUtil.isEmpty(item) || !StringUtil.isDigits(item)) {
                        continue;
                    }
                    portList.add(Integer.valueOf(item));
                }
                if (!portList.isEmpty()) {
                    int[] portIntArray = new int[portList.size()];
                    for (int i = 0; i < portIntArray.length; i++) {
                        portIntArray[i] = portList.get(i);
                    }
                    return portIntArray;
                }
            }
        }
        return null;
    }

    @Override
    public void run() {
        execute();
    }

    public abstract void execute();
}
